#!/usr/bin/env python
#
# Copyright 2008 Allen Hutchison (allen@hutchison.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# $URL: http://severedelays.googlecode.com/svn/trunk/appengine/page_handlers/datacompactor.py $
__version__ = "$Rev: 70 $"
__author__ = "Allen Hutchison (allen@hutchison.org)"



import logging

from lib.data_compactor import DataCompactor
from lib.request_handler import BaseRequest
from lib.validators import Validators

from google.appengine.api.labs import taskqueue
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class DataCompactorPage(BaseRequest):
  """Compact the StatusValue by invoking the DataCompactor

  This script handles all requests for status.severedelays.org/*/datacompactor
  """
  def get(self, path):
    date = self.request.get('date')
    path = "/%sdatacompactor/worker" % path
    for line in Validators.validLines:
      logging.info("Queing Request for %s on %s" % (date, line))
      taskqueue.add(url=path, params={'line': line, 'date': date})

class DataCompactorWorker(BaseRequest):
  """Task Queue worker for the Data Compactor"""
  def post(self, path):
    date = self.request.get('date')
    line = self.request.get('line')
    logging.info("Starting worker for %s on %s" % (line, date))
    dc = DataCompactor(date, line)

application = webapp.WSGIApplication([(r'/(.*/)*datacompactor', DataCompactorPage),
                                      (r'/(.*/)*datacompactor/worker', DataCompactorWorker),
                                    ], debug=True)

def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()